package com.example.demo.autorun;

import com.example.demo.dao.UserDao;
import com.example.demo.model.User;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.Iterator;

/**
 * Created by guofs on 2017/6/28.
 *
 * 在Spring Boot启动时执行该代码
 */
@Component
@Order(value=1)
public class SpringBootAutoRun implements CommandLineRunner {
    private static final Logger LOGGER = Logger.getLogger(SpringBootAutoRun.class);

    @Autowired
    private UserDao userDao;

    @Override
    public void run(String... args) throws Exception {
        LOGGER.info(">>>>>>>>>>>>>>>服务启动执行，执行加载数据等操作<<<<<<<<<<<<<");
        Iterator users = userDao.findAll().iterator();
        if (!users.hasNext()) {
            userDao.save(new User("王伟", Long.valueOf(24l)));
        }
    }
}
//public class SpringBootAutoRun{
//
//}